<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.w3.org/1999/xhtml">

<head>
    <th:block th:include="common/common_head :: commonHeader('货源记录')"/>
    <link rel="stylesheet" th:href="@{/static/js/plugins/jquery-layout/jquery.layout-latest.css}">
</head>

<body class="gray-bg">

<div class="container-div ui-layout-center">
    <div class="row">
        <div class="col-sm-12 search-condition">
            <form id="supplyGoodForm" th:action="@{/supplyGood/list}" class="form-inline">
                <div class="select-list">
                    <ul>
                        <li class="col-sm-3">
                            <label>物料号：</label>
                            <input type="text" name="materialCode"/>
                        </li>
                        <li class="col-sm-3">
                            <label>供应商编码：</label>
                            <input type="text" name="srmCode"/>
                        </li>
                        <li class="col-sm-3">
                            <div>
                                <label>工厂：</label>
                                <select name="werks" sysDict="GC" emptyFirst="true"></select>
                            </div>
                        </li>

                        <li class="col-sm-3">
                            <div>
                                <label>采购组：</label>
                                <select name="purchaseGroup" sysDict="CGZ" emptyFirst="true"></select>
                            </div>
                        </li>


                        <li class="col-sm-3">
                            <div>
                                <label>状态：</label>
                                <select name="infoStatus" emptyFirst="true">
                                    <option></option>
                                    <option value="1">已确认</option>
                                    <option value="0">未确认</option>
                                </select>
                            </div>
                        </li>
                        <li class="col-sm-3">
                            <button id="supplyGoodSearchBtn" type="button" class="btn btn-primary btn-rounded btn-sm"><i
                                    class="fa fa-search"></i>&nbsp;搜索
                            </button>
                            <button class="btn btn-warning btn-rounded btn-sm" type="reset"><i class="fa fa-eraser"></i>&nbsp;重置
                            </button>
                            <button id="supplyGoodAdvancedSearch" type="button"
                                    class="btn btn-primary btn-rounded btn-sm" style="display: none"><i
                                    class="fa fa-caret-down"></i>&nbsp;高级搜索
                            </button>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="col-sm-12 select-info order-table">
            <div class="btn-group hidden-xs toolbar" id="supplyGoodToolBar" role="group">
                <a class="btn btn-outline btn-success btn-rounded" onclick="toAddSupply()">
                    <i class="fa fa-plus"></i> 新增
                </a>
                <a class="btn btn-outline btn-success btn-rounded" onclick="toEdit()"
                   shiro:hasPermission="supplyGoods:edit">
                    <i class="fa fa-edit"></i> 编辑
                </a>
                <a class="btn btn-outline btn-danger btn-rounded" onclick="deleteSupply()"
                   shiro:hasPermission="supplyGoods:delete">
                    <i class="fa fa-edit"></i> 删除
                </a>
                <a class="btn btn-outline btn-success btn-rounded" onclick="confirmInfo()"
                   shiro:hasPermission="supplyGoods:confirmInfo">
                    <i class="fa fa-edit"></i> 确认
                </a>
                <a class="btn btn-outline btn-success btn-rounded" onclick="downloadTemplate()"
                   shiro:hasPermission="supplyGoods:upload">
                    <i class="fa fa-file-excel-o"></i> 模板下载
                </a>
                <a class="btn btn-outline btn-success btn-rounded" onclick="openFileChooser()"
                   shiro:hasPermission="supplyGoods:batchImport">
                    <i class="fa fa-file-excel-o"></i> 批量导入
                </a>

            </div>
            <table id="supplyGood" data-mobile-responsive="true"></table>
        </div>
    </div>
</div>

<form id="xlsForm" style="display:none" enctype="multipart/form-data">
    <input type="file" id="xlsFile" name="xlsFile">
</form>
<!--引入公共js-->
<div th:include="common/onload_js :: onloadJS"></div>
<script type="text/javascript" th:src="@{/static/js/plugins/jquery-layout/jquery.layout-latest.js}"></script>

<!-- Page-Level Scripts -->

<!--  -->
<script type="text/javascript">
    //面向对象
    var searcherBS = Searcher.createBS('#supplyGood',
        [
            {checkbox: true},
            {
                title: '确认', field: 'infoStatus',
                formatter: function (value, row, index) {
                    if (value == 1) {
                        return "<img src='" + ctxPath + "/img/light/green.png' width='20px' />";
                    } else {
                        return "<img src='" + ctxPath + "/img/light/red.png' width='20px' />";
                    }
                }
            },
         /*   {title: '货源记录编号', field: 'supplyNum'},*/
            {title: '物料编码', field: 'materialCode'},
            {title: '物料描述', field: 'materialDesc'},
            {title: '供应商编码', field: 'srmCode'},
            {title: '供应商名称', field: 'vendorName'},
            {title: '工厂', field: 'werks'},
            {title: '采购组', field: 'purchaseGroup'},
            {title: '采购组织', field: 'purchArg'},
            {title: '有效期开始时间', field: 'startData'},
            {title: '有效期结束时间', field: 'priceEndData'}
        ]
    );

    //刷新数据
    function refresh() {
        searcherBS.refresh();
    }


    $("#xlsFile").change(function () {
        var filename = $(this).val();
        if (filename.lastIndexOf(".") >= 0x0) {
            filename = filename.substring(filename.lastIndexOf(".")).toLowerCase();
        } else {
            filename = "";
        }
        if (filename != ".xls" && filename != ".xlsx") {
            Dialog.errorMsg("只允许上传Excel文件！");
            return;
        }
        var formData = new FormData($('#xlsForm')[0]);
        Page.ajaxPOST(ctxPath + "/supplyGood/imp", formData, function (o) {
            //dataGrid.getData({'url': ctxPath + '/vendor/datalList?theID='+o.theID});
            var index = Dialog.openUrl("导入数据", ctxPath + '/modules/inforecord/supplygood_imp_data.html', {
                area: ['800px', '700px'],
                maxmin: true,
                end: function () {
                }
            });
        }, {cache: false, processData: false, async: false, contentType: false, loadingMsg: "正在往服务器上传数据……"});
        $(this).val("");
    });
    //初始化所有字典数据
    initDictDataSel();

    /**
     * 跳转至新增页
     */
    function toAddSupply() {
        var index = Dialog.openUrl("新增货源记录", ctxPath + '/supplyGood/toAdd',
            {
                area: ['900px', '700px'],
                maxmin: true,
                end: function () {
                }
            });
        layer.full(index);
    }

    /**
     * 编辑
     */
    function toEdit() {
        var rowDatas = searcherBS.getSelectedRows("pkId");
        var infoStatus = searcherBS.getSelectedRows("infoStatus");
        let endDate = searcherBS.getSelectedRows("priceEndData");
        if (rowDatas.length != 1) {
            Dialog.errorMsg("请选择一行进行编辑");
            return;
        }
        if (infoStatus != 1) {
            Dialog.errorMsg("未确认的货源记录不能修改");
            return;
        }
        endDate = new Date(endDate);
        let now = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate());
        /* if (now < endDate) {
             Dialog.errorMsg("有效期未到，不能修改！");
             return false;
         }*/
        var index = Dialog.openUrl("编辑", ctxPath + '/supplyGood/toEdit?pkId=' + rowDatas,
            {
                area: ['900px', '700px'],
                maxmin: true,
                end: function () {
                }
            });
        layer.full(index)
    }

    function deleteSupply() {
        var rowDatas = searcherBS.getSelectedRows("pkId");
        if (rowDatas.length < 1) {
            Dialog.errorMsg("请至少选择一行");
            return;
        }
        var infoStatus = searcherBS.getSelectedRows("infoStatus");
        for (var i in infoStatus) {
            if (infoStatus[i] == 1) {
                Dialog.errorMsg("已经确认的信息记录不能删除");
                return;
            }
        }
        var toSendSap = searcherBS.getSelectedRows("toSendSap");
        for (var i in infoStatus) {
            if (toSendSap[i] == 1) {
                Dialog.errorMsg("已经同步sap的不能删除");
                return;
            }
        }
        Dialog.confirm('确定删除？', function (index) {
            Page.ajaxPOST(ctxPath + "/supplyGood/delete", {userIds: rowDatas}, function () {
                Dialog.msg("删除成功", function () {
                    searcherBS.refresh();
                });
            }, {"loadingMsg": "正在往服务器提交数据……"});
        });
    }

    /**
     * 确认采购员编辑的信息记录，确认后，后台同步sap
     */
    function confirmInfo() {
        //获取id
        var pkId = searcherBS.getSelectedRows("pkId");
        var status = searcherBS.getSelectedRows("infoStatus");
        if (pkId.length == 0) {
            Dialog.errorMsg("请选择信息记录进行操作");
            return;
        }
        for (var i in status) {
            if (status[i] == 1) {
                Dialog.errorMsg("该记录已确认过！");
                return;
            }
        }

        $.ajax({
            url: ctxPath + '/supplyGood/confirmInfo/' + pkId,
            dataType: "json",
            success: function (data) {
                if (data.code == 0) {
                    Dialog.msg(data.msg);
                    refresh();
                } else {
                    Dialog.errorMsg(data.msg);
                }
            },
            error: function (data) {

            }
        });
    }

    /**
     * 点击“批量导入”按钮，打开文件选择框
     **/
    function openFileChooser() {
        $("#xlsFile").click();
    }

    /**
     * 模板下载
     */

    function downloadTemplate() {
        window.location.href = ctxPath + "/impTemplate/supplyGoods.xls";
    }

</script>
</body>
</html>
